package com.google.zxing.util;

import android.annotation.SuppressLint;
import android.annotation.TargetApi;
import android.app.AppOpsManager;
import android.content.Context;
import android.os.Binder;
import android.os.Build;
import android.os.Build.VERSION;
import android.os.Environment;
import android.text.TextUtils;
import android.util.Log;

import androidx.annotation.Nullable;

import java.io.File;
import java.io.FileInputStream;
import java.lang.reflect.Method;
import java.util.Properties;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/*
 * created by wdlmm1214
 * on 2019/12/23
 */
@SuppressLint({"PrivateApi"})
public class QMUIDeviceHelper {
    private static final String TAG = "QMUIDeviceHelper";
    private static final String KEY_MIUI_VERSION_NAME = "ro.miui.ui.version.name";
    private static final String KEY_FLYME_VERSION_NAME = "ro.build.display.id";
    private static final String FLYME = "flyme";
    private static final String ZTEC2016 = "zte c2016";
    private static final String ZUKZ1 = "zuk z1";
    private static final String ESSENTIAL = "essential";
    private static final String[] MEIZUBOARD = new String[]{"m9", "M9", "mx", "MX"};
    private static String sMiuiVersionName;
    private static String sFlymeVersionName;
    private static boolean sIsTabletChecked = false;
    private static boolean sIsTabletValue = false;
    private static final String BRAND;

    public QMUIDeviceHelper() {
    }

    private static boolean _isTablet(Context context) {
        return (context.getResources().getConfiguration().screenLayout & 15) >= 3;
    }

    public static boolean isTablet(Context context) {
        if (sIsTabletChecked) {
            return sIsTabletValue;
        } else {
            sIsTabletValue = _isTablet(context);
            sIsTabletChecked = true;
            return sIsTabletValue;
        }
    }

    public static boolean isFlyme() {
        return !TextUtils.isEmpty(sFlymeVersionName) && sFlymeVersionName.contains("flyme");
    }

    public static boolean isMIUI() {
        return !TextUtils.isEmpty(sMiuiVersionName);
    }

    public static boolean isMIUIV5() {
        return "v5".equals(sMiuiVersionName);
    }

    public static boolean isMIUIV6() {
        return "v6".equals(sMiuiVersionName);
    }

    public static boolean isMIUIV7() {
        return "v7".equals(sMiuiVersionName);
    }

    public static boolean isMIUIV8() {
        return "v8".equals(sMiuiVersionName);
    }

    public static boolean isMIUIV9() {
        return "v9".equals(sMiuiVersionName);
    }

    public static boolean isFlymeVersionHigher5_2_4() {
        boolean isHigher = true;
        if (sFlymeVersionName != null && !sFlymeVersionName.equals("")) {
            Pattern pattern = Pattern.compile("(\\d+\\.){2}\\d");
            Matcher matcher = pattern.matcher(sFlymeVersionName);
            if (matcher.find()) {
                String versionString = matcher.group();
                if (versionString != null && !versionString.equals("")) {
                    String[] version = versionString.split("\\.");
                    if (version.length == 3) {
                        if (Integer.valueOf(version[0]) < 5) {
                            isHigher = false;
                        } else if (Integer.valueOf(version[0]) > 5) {
                            isHigher = true;
                        } else if (Integer.valueOf(version[1]) < 2) {
                            isHigher = false;
                        } else if (Integer.valueOf(version[1]) > 2) {
                            isHigher = true;
                        } else if (Integer.valueOf(version[2]) < 4) {
                            isHigher = false;
                        } else if (Integer.valueOf(version[2]) >= 5) {
                            isHigher = true;
                        }
                    }
                }
            }
        }

        return isMeizu() && isHigher;
    }

    public static boolean isMeizu() {
        return isPhone(MEIZUBOARD) || isFlyme();
    }

    public static boolean isXiaomi() {
        return Build.MANUFACTURER.toLowerCase().equals("xiaomi");
    }

    public static boolean isVivo() {
        return BRAND.contains("vivo") || BRAND.contains("bbk");
    }

    public static boolean isOppo() {
        return BRAND.contains("oppo");
    }

    public static boolean isHuawei() {
        return BRAND.contains("huawei") || BRAND.contains("honor");
    }

    public static boolean isEssentialPhone() {
        return BRAND.contains("essential");
    }

    public static boolean isZUKZ1() {
        String board = Build.MODEL;
        return board != null && board.toLowerCase().contains("zuk z1");
    }

    public static boolean isZTKC2016() {
        String board = Build.MODEL;
        return board != null && board.toLowerCase().contains("zte c2016");
    }

    private static boolean isPhone(String[] boards) {
        String board = Build.BOARD;
        if (board == null) {
            return false;
        } else {
            String[] var2 = boards;
            int var3 = boards.length;

            for(int var4 = 0; var4 < var3; ++var4) {
                String board1 = var2[var4];
                if (board.equals(board1)) {
                    return true;
                }
            }

            return false;
        }
    }

    public static boolean isFloatWindowOpAllowed(Context context) {
        int version = VERSION.SDK_INT;
        if (version >= 19) {
            return checkOp(context, 24);
        } else {
            try {
                return (context.getApplicationInfo().flags & 134217728) == 134217728;
            } catch (Exception var3) {
                var3.printStackTrace();
                return false;
            }
        }
    }

    @TargetApi(19)
    private static boolean checkOp(Context context, int op) {
        int version = VERSION.SDK_INT;
        if (version >= 19) {
            AppOpsManager manager = (AppOpsManager)context.getSystemService(Context.APP_OPS_SERVICE);

            try {
                Method method = manager.getClass().getDeclaredMethod("checkOp", Integer.TYPE, Integer.TYPE, String.class);
                int property = (Integer)method.invoke(manager, op, Binder.getCallingUid(), context.getPackageName());
                return 0 == property;
            } catch (Exception var6) {
                var6.printStackTrace();
            }
        }

        return false;
    }

    @Nullable
    private static String getLowerCaseName(Properties p, Method get, String key) {
        String name = p.getProperty(key);
        if (name == null) {
            try {
                name = (String)get.invoke((Object)null, key);
            } catch (Exception var5) {
            }
        }

        if (name != null) {
            name = name.toLowerCase();
        }

        return name;
    }

    static {
        BRAND = Build.BRAND.toLowerCase();
        Properties properties = new Properties();
        FileInputStream fileInputStream;
        if (VERSION.SDK_INT < 26) {
            fileInputStream = null;

            try {
                fileInputStream = new FileInputStream(new File(Environment.getRootDirectory(), "build.prop"));
                properties.load(fileInputStream);
            } catch (Exception var8) {
                Log.d("QMUIDeviceHelper", "read file error", var8);
            } finally {
                QMUILangHelper.close(fileInputStream);
            }
        }

        fileInputStream = null;

        try {
            Class<?> clzSystemProperties = Class.forName("android.os.SystemProperties");
            Method getMethod = clzSystemProperties.getDeclaredMethod("get", String.class);
            sMiuiVersionName = getLowerCaseName(properties, getMethod, "ro.miui.ui.version.name");
            sFlymeVersionName = getLowerCaseName(properties, getMethod, "ro.build.display.id");
        } catch (Exception var7) {
            Log.d("QMUIDeviceHelper", "read SystemProperties error", var7);
        }

    }
}
